package br.com.unieuro.servlet;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import com.google.gson.Gson;

import br.com.unieuro.dominio.Item;
import br.com.unieuro.dominio.Usuario;
import br.com.unieuro.facade.ManterItemFacade;
import br.com.unieuro.facade.ManterUsuarioFacade;
import br.com.unieuro.servico.CriarJson;
import br.com.unieuro.servico.Criptografia;

public class FrontControllerUsuario extends HttpServlet{
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{

		Usuario u = new Usuario();
		u.setNome(req.getParameter("login"));
		u.setSenha(Criptografia.getHashString(req.getParameter("senha")));
		ManterUsuarioFacade facade = new ManterUsuarioFacade();
		u = facade.autenticarUsuario(u);

		if(u != null){
			Cookie c = new Cookie("login", u.getNome());
			resp.addCookie(c);
			req.setAttribute("perfil", u.getPerfil());
			HttpSession session = req.getSession();
			session.setAttribute("usuario", u);
			RequestDispatcher disp = req.getRequestDispatcher("jsp/home.jsp");
			disp.forward(req, resp);
		}else{
			resp.sendRedirect("jsp/loginnot.jsp");
		}
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
		String pagina = req.getParameter("pagina");
                                                                                                                            
		if((pagina != null) && (pagina.equals("listarUsuario"))){
			ManterUsuarioFacade facade = new ManterUsuarioFacade();
			ArrayList<Usuario> users = facade.pesquisarTodosUsuarios();
			Gson gson = new Gson();
			String json = gson.toJson(users);
			req.getSession().setAttribute("listaUsuarios",json);

			resp.sendRedirect("jsp/listarUsuario.jsp");
		}
		if((pagina != null) && (pagina.equals("gerenciarUsuario"))){
			ManterUsuarioFacade facade = new ManterUsuarioFacade();
			ArrayList<Usuario> users = facade.pesquisarTodosUsuarios();
			Gson gson = new Gson();
			String json = gson.toJson(users);
			req.getSession().setAttribute("listaUsuarios",json);
			System.out.println(json);
			resp.sendRedirect("jsp/gerenciarUsuario.jsp");
		}
		if((pagina != null) && (pagina.equals("cadastrarUsuario"))){
			Usuario user = new Usuario();
			user.setNome(req.getParameter("login"));
			user.setCpf(req.getParameter("cpf"));
			user.setSenha(Criptografia.getHashString(req.getParameter("passwd")));
			user.setPerfil(req.getParameter("perfil"));
			user.setMatricula(Integer.valueOf(req.getParameter("matricula")));
			ManterUsuarioFacade facade = new ManterUsuarioFacade();
			facade.salvar(user);
			
			resp.sendRedirect("jsp/gerenciarUsuario.jsp");

		}
		if((pagina != null) && (pagina.equals("editarExcluirPessoa"))){
			Usuario user = new Usuario();
			Usuario u = new Usuario();
			ManterUsuarioFacade facade = new ManterUsuarioFacade();
			String gerenciar = req.getParameter("editarUsuario");
	
			if (gerenciar.equals("Buscar")){
				user.setId(Integer.valueOf(req.getParameter("id")));
				u = facade.buscarUsuario(user);
				System.out.println(u.getNome());
				//user = facade.buscarUsuario(user);
				//req.getSession().setAttribute("listUser",user);
			}
			
			if (gerenciar.equals("Excluir")){
				user.setId(Integer.valueOf(req.getParameter("id")));
				facade.excluir(user); 
			}
			if (gerenciar.equals("Alterar")){
				user.setId(Integer.valueOf(req.getParameter("id")));
				user.setNome(req.getParameter("login"));
				user.setCpf(req.getParameter("cpf"));
				user.setSenha(Criptografia.getHashString(req.getParameter("passwd")));
				user.setPerfil(req.getParameter("perfil"));
				user.setMatricula(Integer.valueOf(req.getParameter("matricula")));
				facade.update(user);
		}
			resp.sendRedirect("jsp/gerenciarUsuario.jsp");
	}
}
}
